package dev.ece.suit.loader.models.fbx;

import org.apache.commons.lang3.StringUtils;

import dev.ece.util.lib.Matrix4f;
import dev.ece.util.lib.Vector3f;
import dev.ece.util.lib.Vector4f;

public class FBXMeshModel extends FBXModel {

	public static final String GEOMETRIC_TRANSLATION = "GeometricTranslation";
	
	public static final String GEOMETRIC_ROTATION = "GeometricRotation";
	
	public static final String GEOMETRIC_SCALING = "GeometricScaling";
	
	private Vector3f geometricTranslation = new Vector3f();
	
	private Vector4f geometricRotation = new Vector4f();
	
	private Vector3f geometricScaling = new Vector3f(1, 1, 1);
	
	private Matrix4f geometricMatrix = new Matrix4f();

	@Override
	protected void onChildProperties(String type, FBXElement child) {
		if(StringUtils.equals(type, GEOMETRIC_TRANSLATION)) {
			geometricTranslation.set(toVector3f(child));
		} else if(StringUtils.equals(type, GEOMETRIC_ROTATION)) {
			Vector3f rotation = toVector3f(child);
			geometricRotation.fromEulerToQuaternion(
					(float) Math.toRadians(rotation.getX()),
					(float) Math.toRadians(rotation.getY()),
					(float) Math.toRadians(rotation.getZ()));
		} else if(StringUtils.equals(type, GEOMETRIC_SCALING)) {
			geometricScaling.set(toVector3f(child));
		}
	}

	protected void setup() {
		super.setup();
		this.getGeometricMatrix().identity();
		Matrix4f t = new Matrix4f();
		t.identity();
		t.translate(this.getGeometricTranslation());
		Matrix4f r = new Matrix4f();
		r.identity();
		r.rotateQuaternion(this.getGeometricRotation());
		Matrix4f s = new Matrix4f();
		s.identity();
		s.scale(this.getGeometricScaling());
		this.getGeometricMatrix().mul(t, r);
		this.getGeometricMatrix().mul(s);
	}

	protected Matrix4f getGeometricMatrix() {
		return geometricMatrix;
	}

	public Vector3f getGeometricTranslation() {
		return geometricTranslation;
	}

	public Vector4f getGeometricRotation() {
		return geometricRotation;
	}

	public Vector3f getGeometricScaling() {
		return geometricScaling;
	}

}
